﻿#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
#include <iostream>
// hung begin
void input(char *a, char *b) // Nhap
{
	printf("Nhap chuoi so thư 1");
	scanf("%c",a);
	printf("Nhap chuoi so thư 2");
	scanf("%c",b);
}
//hung end


//tuonglv begin
char* catstr(char *s,char *d) 
{
	int length = strlen(s) + strlen(d) + 1;
	char *returnStr = new char[length];
	strcpy(returnStr,s);
	strcat(returnStr,d);
	returnStr[length] = '\0';
	return returnStr;
}
void convertLength(char *&a,char *&b) //Dua 2 c chuoi cung chieu dai
{
	int la = strlen(a);
	int lb = strlen(b);
	if (la > lb)
	{
		for (int i=0;i<la-lb;i++)
		{
			b = catstr("0",b);
		}
	}
	else
	{
		for (int i=0;i<lb-la;i++)
		{
			a = catstr("0",a);
		}
	}

}
//tuonglv end

//dangkhoi begin

char *getSum(char *a, char *b) // Tinh tong
{
	int code0 = 48;
	int length = strlen(a);
	char *result = new char[length + 1];
	char *a1 = a;
	strrev(a1);
	char *a2 = b;
	strrev(a2);

	int temp = 0;
	
	for(int i = 0; i < length; ++i)
	{
		if (a[i] - code0 + b[i] - code0 + temp <= 9)
		{
			result[i] = a[i] + b[i] + temp - code0;
			temp = 0;
		}
		else
		{
			result[i] = (a[i] - code0 + b[i] - code0 + temp) % 10 + code0;
			temp = 1;
		}
	}
	if( temp == 1)
		result[length] = code0 + 1;
	result[length+1] = '\0';
	strrev(result);
	return result;
}
//dangkhoi end;

// Van Anh begin
 int checkError(char *a) //Kiem tra loi
 {
	 int i,n=strlen(a);
	 
	 for( i=0;i<n;i++)
	 {
		 if(a[i]<'0'||a[i]>'9') break;
	 }
	 if(i==n )
	 {
		 return 1;
	 }
	 else return 0;
 }
 //Van Anh end

 //hung begin 
void output(char *sum) // Xuat ra man hinh
{
	printf("Ket qua:%s",&sum);
}
//hung end

//ba hung begin
int main()
{
	char *a = new char[100];
	char *b = new char[100];
	flushall();

	cout<<"nhap so thu nhat :";
	cin.getline(a,100);

	cout<<"nhap so thu hai :";
	cin.getline(b,100);

	convertLength(a,b);
	cout<<"Ket qua: "<<getSum(a,b);
	getch();
	return 0;
}
//ba hung end